package cn.sdormitory.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.vo.BStudentListVo;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.constant.CommonConstant;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.service.AppAttenceExceptionService;
import cn.sdormitory.service.AppAttenceService;
import cn.sdormitory.service.AppStudentService;
import cn.sdormitory.service.AppUserService;
import cn.sdormitory.smartdor.entity.DeptDataBoardSummary;
import cn.sdormitory.smartdor.entity.DeptDormitorySummary;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.entity.SdAttenceException;
import cn.sdormitory.smartdor.vo.DormitoryAttenceVo;
import cn.sdormitory.smartdor.vo.SdAttenceVo;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @创建人：zhouyang
 * @创建时间：2020/12/30 14:44
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/Attence")
@Api("APP端考勤管理接口")
public class AppAttenceController {
    @Autowired
    private AppAttenceService appAttenceService;
    @Autowired
    private AppAttenceExceptionService appAttenceExceptionService;
    @Autowired
    private AppStudentService appStudentService;
    @Autowired
    private AppUserService appUserService;

    @ApiOperation("listAbsenceStu => APP端缺勤列表")
    @GetMapping(value = "/listAbsenceStu")
    public ResultMsg listAbsenceStu(String checkDate) throws ParseException {
        List<SdAttenceVo> sdAttenceVoList=new ArrayList<SdAttenceVo>();
        if(!"".equals(checkDate) && checkDate!=null){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
            Date date = simpleDateFormat.parse(checkDate);
            sdAttenceVoList=appAttenceService.getAppAbsenceStudent(date);
        }else{
            sdAttenceVoList=appAttenceService.getAppAbsenceStudent(null);
        }

        return ResultMsg.BY_SUCCESS("获取成功",sdAttenceVoList);
    }
    
    @ApiOperation("listDormitoryStu => APP端宿舍考勤缺勤列表  -部门经理查看")
    @GetMapping(value = "/listDormitoryStu")
    public ResultMsg listDormitoryStu(String userId, String checkDate, String classId) {
        if (classId.equals("undefined")) {
            classId = null;
        }
        List<BStudentListVo> sdAttenceVos = appAttenceService.getDormitoryUnClockStu(userId, checkDate, classId);
        return ResultMsg.BY_SUCCESS("获取成功", sdAttenceVos);
    }
    
    @ApiOperation("getDormitoryUnclockClassList => APP端宿舍考勤缺勤班级列表  -部门经理查看")
    @GetMapping(value = "/getDormitoryUnclockClassList")
    public ResultMsg getDormitoryUnclockClassList(String userId, String checkDate) {
        List<BStudentListVo> sdAttenceVos = appAttenceService.getDormitoryUnClockStu(userId, checkDate, null);
        List<Map<String, String>> classList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(sdAttenceVos)) {
            for (BStudentListVo sdAttenceVo : sdAttenceVos) {
                Map<String, String> classMap = new HashMap<>();
                classMap.put("value", sdAttenceVo.getClassId());
                classMap.put("text", sdAttenceVo.getClassName());
                classList.add(classMap);
            }
            classList = classList.stream().distinct().collect(Collectors.toList());
        }
        return ResultMsg.BY_SUCCESS("获取成功", classList);
    }
    
    @ApiOperation("getWeekendUnclockClassList => APP端周末考勤缺勤班级列表  -部门经理查看")
    @GetMapping(value = "/getWeekendUnclockClassList")
    public ResultMsg getWeekendUnclockClassList(String userId, String checkDate) {
        List<BStudentListVo> sdAttenceVos = appAttenceService.getWeekendUnClockStu(userId, checkDate, null);
        List<Map<String, String>> classList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(sdAttenceVos)) {
            for (BStudentListVo sdAttenceVo : sdAttenceVos) {
                Map<String, String> classMap = new HashMap<>();
                classMap.put("value", sdAttenceVo.getClassId());
                classMap.put("text", sdAttenceVo.getClassName());
                classList.add(classMap);
            }
            classList = classList.stream().distinct().collect(Collectors.toList());
        }
        return ResultMsg.BY_SUCCESS("获取成功", classList);
    }
    
    
    @ApiOperation("listWeekendUnclockStu => APP端周末考勤缺勤列表  -部门经理查看")
    @GetMapping(value = "/listWeekendUnclockStu")
    public ResultMsg listWeekendUnclockStu(String userId, String checkDate, String classId) {
        if (classId.equals("undefined")) {
            classId = null;
        }
        List<BStudentListVo> sdAttenceVos = appAttenceService.getWeekendUnClockStu(userId, checkDate, classId);
        return ResultMsg.BY_SUCCESS("获取成功", sdAttenceVos);
    }

    @ApiOperation("queryException => APP端缺勤数据对应的异常处理数据")
    @GetMapping(value = "/queryException")
    public ResultMsg queryException(String studentNo,String accessDate)  {
      SdAttenceException sdAttenceException=appAttenceExceptionService.getAppException(studentNo,accessDate);
        return ResultMsg.BY_SUCCESS("获取成功",sdAttenceException);
    }

    @ApiOperation("getAttenceExcById => APP端根据考勤ID查询对应的异常处理数据")
    @GetMapping(value = "/getAttenceExcById")
    public ResultMsg getAttenceExcById(Long id)  {
        SdAttence sdAttence=appAttenceService.getAttenceById(id);
        Date accessDate=sdAttence.getAccessDate();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String accDate=sdf.format(accessDate);
        SdAttenceException sdAttenceException=appAttenceExceptionService.getAppException(sdAttence.getStudentNo(),accDate);
        return ResultMsg.BY_SUCCESS("获取成功",sdAttenceException);
    }

    @ApiOperation("getAppExcList => APP端异常处理列表")
    @GetMapping(value = "/getAppExcList")
    public ResultMsg getAppExcList()  {
        List<SdAttenceException> attenceExceptionList=appAttenceExceptionService.getAppExcList();
        return ResultMsg.BY_SUCCESS("获取成功",attenceExceptionList);
    }

    @ApiOperation("getAppExcById => APP端异常处理详情")
    @GetMapping(value = "/getAppExcById")
    public ResultMsg getAppExcDetail(Long id)  {
        SdAttenceException attenceException=appAttenceExceptionService.getAppExceptionById(id);
        return ResultMsg.BY_SUCCESS("获取成功",attenceException);
    }

    @ApiOperation("getClassListAll => APP端获取有效班级列表")
    @GetMapping(value = "/getClassListAll")
    public ResultMsg getClassListAll()  {
        List<BClass> bClassList=appAttenceService.getClassListAll();
        return ResultMsg.BY_SUCCESS("获取成功",bClassList);
    }

    @ApiOperation("getProStatusListAll => APP端获取有效异常处理状态列表")
    @GetMapping(value = "/getProStatusListAll")
    public ResultMsg getProStatusListAll()  {
        String dicType="absence_process_status";
        List<SysDictDetail> sysDictDetailList=appAttenceService.selectDictDataByType(dicType);
        return ResultMsg.BY_SUCCESS("获取成功",sysDictDetailList);
    }


    @ApiOperation("addExc => APP端新增异常处理")
    @PostMapping("/addExc")
    public ResultMsg addExc(String studentNo, Long classId,String studentName,String absenceProcessStatus,String processDesc,Long processPersonId){
        //判断学号是否存在
        BStudent bStudent=appStudentService.getByStudentNo(studentNo);
        if("".equals(bStudent) || bStudent==null){
            return ResultMsg.BY_ERROR("此学号不存在!!!",-1);
        }
        //判断学号+学生姓名+班级是否匹配
        BStudent student=appStudentService.getByStuNoAndNameAndClassId(studentNo,studentName,classId);
        if(student==null){
            return ResultMsg.BY_ERROR("输入的学号、班级、姓名不匹配!!!",-2);
        }
        if(!"".equals(bStudent) && bStudent!=null) {
            //每个学员每天应只有一条考勤异常处理记录
            SdAttenceException exceptionInfo = appAttenceExceptionService.getByStuNoAndDate(studentNo);
            if (exceptionInfo != null) {
                return ResultMsg.BY_ERROR("该学生当天考勤异常已处理!!!",-3);
            }
        }
        //根据班级ID查询班级名称
        BClass bClass=appAttenceService.getClassById(classId);
        //根据用户ID查询用户名称(处理人)
        SysUser sysUser=appUserService.getUserById(new Long(processPersonId).intValue());

        SdAttenceException sdAttenceException=new SdAttenceException();
        sdAttenceException.setStudentNo(studentNo);
        sdAttenceException.setClassId(classId);
        sdAttenceException.setClassName(bClass.getClassName());
        sdAttenceException.setAbsenceProcessStatus(absenceProcessStatus);
        sdAttenceException.setProcessDesc(processDesc);
        sdAttenceException.setStudentName(studentName);
        sdAttenceException.setProcessPerson(sysUser.getUsername());
        sdAttenceException.setAbsenceProcessStatus(absenceProcessStatus);
        sdAttenceException.setStatus(CommonConstant.VALID_STATUS);
        sdAttenceException.setCreateTime(new Date());
        sdAttenceException.setModifyTime(new Date());

        int result=appAttenceExceptionService.insert(sdAttenceException);
        return  ResultMsg.BY_SUCCESS("异常处理新增成功!!!",sdAttenceException);
    }

    @ApiOperation("deleteExc => APP端删除异常处理")
    @GetMapping(value = "/deleteExc")
    public ResultMsg deleteExc(Long id)  {
        int result=appAttenceExceptionService.delete(id);
        return ResultMsg.BY_SUCCESS("删除成功",result);
    }

    @ApiOperation("editExc => APP端修改异常处理")
    @PostMapping("/editExc")
    public ResultMsg editExc(Long id,String absenceProcessStatus,String processDesc){
        int result=appAttenceExceptionService.updateAttenceExc(id,absenceProcessStatus,processDesc);
        return  ResultMsg.BY_SUCCESS("异常处理修改成功!!!",result);
    }

    @ApiOperation("getAllClassAttendanceByClsTeachId => 根据当前班主任id获取其所有管理班级的考勤数据")
    @PostMapping("/getAllClassAttendanceByClsTeachId")
    public CommonResult getAllClassAttendanceByClsTeachId(@RequestBody Map<String,String> params){
        List<Map<String, Object>>  result = appAttenceService.getAllClassAttendanceByClsTeachId(params);
        return CommonResult.success(result);
    }
    
    @ApiOperation("getDeptDormitorySummary => APP端考勤汇总列表  -部门经理查看")
    @GetMapping(value = "/getDeptDormitorySummary")
    public ResultMsg getDeptDormitorySummary(String userId, String accessDate, String operateType) {
        List<DeptDormitorySummary> dormitorySummaries = appAttenceService.getDeptDormitorySummary(userId, accessDate, operateType);
        return ResultMsg.BY_SUCCESS("获取成功", dormitorySummaries);
    }
    
    @ApiOperation("getAttenceBoardData => 部门经理获取数据看板数据")
    @GetMapping(value = "/getAttenceBoardData")
    public ResultMsg getAttenceBoardData(String userId, String type) {
        DeptDataBoardSummary attenceBoardData = appAttenceService.getAttenceBoardData(userId, type);
        return ResultMsg.BY_SUCCESS("获取成功", attenceBoardData);
    }
    
    @ApiOperation("getAttenceStatisticalData => 部门经理每日考勤数据看板")
    @GetMapping(value = "/getAttenceStatisticalData")
    public ResultMsg getAttenceStatisticalData(String userId, String accessDate, String type) {
        DeptDormitorySummary attenceBoardData = appAttenceService.getAttenceStatisticalData(userId, accessDate, type);
        return ResultMsg.BY_SUCCESS("获取成功", attenceBoardData);
    }
    
    @ApiOperation("getStuListByType => 获取学生列表根据类型")
    @GetMapping("/getStuListByType")
    public ResultMsg getStuListByType(String classId, String accessDate, String type, String operateType) {
        List<BStudentListVo> studentListVos = appAttenceService.getStuListByType(classId, accessDate, type);
        return ResultMsg.BY_SUCCESS("获取成功", studentListVos);
    }
    
    @ApiOperation("getStuListByType => 周末打卡获取学生列表根据类型")
    @GetMapping("/getWeekendStuListByType")
    public ResultMsg getWeekendStuListByType(String classId, String accessDate, String type, String operateType) {
        List<BStudentListVo> studentListVos = appAttenceService.getWeekendStuListByType(classId, accessDate, type);
        return ResultMsg.BY_SUCCESS("获取成功", studentListVos);
    }
    
    @ApiOperation("getMonthlyStatisticalData => 部门经理月度数据看板")
    @GetMapping(value = "/getMonthlyStatisticalData")
    public ResultMsg getMonthlyStatisticalData(String userId, String type) {
        List<DeptDataBoardSummary> deptDataBoardSummary = appAttenceService.getMonthlyStatisticsData(userId, type);
        return ResultMsg.BY_SUCCESS("获取成功", deptDataBoardSummary);
    }
}
